home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / lise / src / genspc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-31  |  3.7 KB  |  149 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <spec.h>
  4.  
  5. int umax,ulen,ubeg,uend;
  6. float   *spc,*err,*tim;
  7.  
  8. char z[80],comment[80],uname[80];
  9.  
  10.  
  11. help()
  12. {
  13. printf("generate spectrum\n");
  14. printf("genspc sp[{i:j}] {-lin a b} {-sin a b c} {-exp a b c}\n");
  15. printf("contrary to the normal behaviour, the WHOLE spectrum (and corresponding\n");
  16. printf("error array) is stored back \n\n");
  17.  
  18. printf("   -t    generate timespectrum only. \n");
  19. printf("   -e    generate errorspectrum only. \n");
  20. printf("   -m n  maximum of n elements \n\n");
  21.  
  22. printf("   -sin a b c     for all n: spc(n) = a*sin(b*n+c)) \n");
  23. printf("   -exp a b c     for all n: spc(n) = a+b*exp(c*n) \n");
  24. printf("   -lin a b       for all n: spc(n) = a+n*b\n");
  25. }
  26.  
  27. main(argc,argv)
  28. int argc;
  29. char *argv[];
  30. {
  31. int   m,n,i,max;
  32. char  s[80],z[80];
  33. FILE  *stream,*fopen();
  34. float a,b,c,reals[30];
  35.  
  36.    if(argc<3) _help0(); /* at least two parameters should be specified */
  37.    if(checkopt(argc,argv,"-dummy",s)) printf("\n"); /* get standard options */
  38.  
  39.    spc= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
  40.    err= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
  41.    tim= (float *)calloc(_MAXSPCLEN+2,sizeof(float));
  42.    if(tim==NULL) {
  43.       printf("sorry, not enough memory\n");
  44.       exit(-1);
  45.    }
  46.  
  47.    _tica = 1.0;                        /* set time calibration */
  48.    ubeg=0;
  49.    uend=_MAXSPCLEN;
  50.    max=_MAXSPCLEN;
  51.  
  52.    strcat(uname,argv[1]);              /* get spectrum name */
  53.    i=instr("[",uname);            /* check for spectrum name  */
  54.    if(i>0) {
  55.       midstr(uname,argv[1],0,i-1);
  56.       midstr(s,argv[1],i+1,instr("]",argv[1])-1);
  57.    } else {
  58.       strcpy(s,"");
  59.    }
  60.    n=instr(":",s);
  61.    if(strlen(s)>0) {             /* handle short spectrum */
  62.       if(n<0) {                  /* spc[n] */
  63.          uend=atoi(s);
  64.          ubeg=0;
  65.          max=uend;
  66.       }
  67.       if(n>0) {                  /* spc[a:b] */
  68.          midstr(z,s,0,n-1); ubeg=atoi(z);
  69.          midstr(z,s,i+1,strlen(s)); uend=atoi(z);
  70.          max=uend;
  71.          for(i=0;i<=ubeg;i++) spc[i]=0.0;
  72.       }
  73.    }
  74.    if(uname[0]=='-') strcpy(uname,"");
  75.  
  76.    if(checkopt(argc,argv,"-m",s)) max=atoi(s);
  77.  
  78.  
  79. /* ----------------------------------------------------------------------
  80.          generate the arrays
  81.    ---------------------------------------------------------------------- */
  82.    if(xcheckopt(argc,argv,"-lin",reals)) {
  83.       a=reals[0]; b=reals[1];
  84.       n=0;
  85.       for(i=ubeg;i<=uend;i++) {
  86.          spc[i] = a + (n * b);
  87.          n=n+1;
  88.       }
  89.       sprintf(comment,"lin: %E + (%E * x)",a,b);
  90.    }
  91.  
  92.    if(xcheckopt(argc,argv,"-sin",reals)) {
  93.       a=reals[0]; b=reals[1]; c=reals[2];
  94.       n=0;
  95.       for(i=ubeg;i<=uend;i++) {
  96.          spc[i] = a * sin((b * n) + c);
  97.          n=n+1;
  98.       }
  99.       sprintf(comment,"sin: %E * sin((%E * x) + %E)",a,b,c);
  100.    }
  101.  
  102.    if(xcheckopt(argc,argv,"-exp",reals)) {
  103.       a=reals[0]; b=reals[1]; c=reals[2];
  104.       n=0;
  105.       for(i=ubeg;i<=uend;i++) {
  106.          spc[i] = a + (b * exp(n*c));
  107.          n=n+1;
  108.       }
  109.       sprintf(comment,"exp: %E + (%E * exp(%E * x))",a,b,c);
  110.    }
  111.  
  112. /* ----------------------------------------------------------------------
  113.          save spectra
  114.    ---------------------------------------------------------------------- */
  115.  
  116.  
  117.    if(checkopt(argc,argv,"-t",z)) { /* modify time spectrum only */
  118.       strcat(uname,".tim");
  119.    }
  120.    if(checkopt(argc,argv,"-e",z)) { /* modify error spectrum only */
  121.       strcat(uname,".err");
  122.    }
  123.  
  124.    writespec(uname,spc,err,max,2,comment);
  125.    free(err); free(spc); free(tim);
  126.    exit(0);
  127. }
  128.  
  129. xcheckopt(argc,argv,s,reals)
  130. int argc;
  131. char *argv[],s[];
  132. float reals[];
  133. {
  134. int i,n,m,erg;
  135. char c;
  136.  
  137.    erg=FALSE;
  138.    i=1; m=0;
  139.    while(i<=argc) {
  140.       if(strcmp(argv[i++],s)==0) {
  141.          erg=TRUE;
  142.          for(n=i;n<=argc;n++) reals[m++]=atosf(argv[n]);
  143.          break;
  144.       }
  145.    }
  146.    return(erg);
  147. }
  148.          
  149.